#region Using

using System.Collections;
using System.Windows.Forms;

#endregion

namespace Match.ManagementApplication.Classes
{
    public class ListViewColumnSorter : IComparer
    {
        #region Private Member

        private int ColumnToSort;
        private CaseInsensitiveComparer ObjectCompare;
        private SortOrder OrderOfSort;

        #endregion

        #region Constructor

        public ListViewColumnSorter(int Column, SortOrder Order)
        {
            ColumnToSort = Column;
            OrderOfSort = Order;
            ObjectCompare = new CaseInsensitiveComparer();
        }

        #endregion

        #region Public Methods

        public int Compare(object x, object y)
        {
            int compareResult;
            ListViewItem listviewX, listviewY;
            listviewX = (ListViewItem)x;
            listviewY = (ListViewItem)y;

            compareResult =
                ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);

            if (OrderOfSort == SortOrder.Ascending)
            {
                return compareResult;
            }
            else if (OrderOfSort == SortOrder.Descending)
            {
                return (-compareResult);
            }
            else
            {
                return 0;
            }
        }

        #endregion

        #region Public Properties

        public int SortColumn
        {
            set { ColumnToSort = value; }
            get { return ColumnToSort; }
        }

        public SortOrder Order
        {
            set { OrderOfSort = value; }
            get { return OrderOfSort; }
        }

        #endregion
    }
}